import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "path";
//引入svg需要用到的插件
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
import { viteMockServe } from "vite-plugin-mock";
export default defineConfig(({mode}) => {
  //获取各种环境下对应的变量
  let env = loadEnv(mode, process.cwd());
  return {
    //获取各种环境下的对应的变量
    plugins: [
      vue(),
      createSvgIconsPlugin({
        iconDirs: [path.resolve(process.cwd(), "src/assets/icons")],
        symbolId: "icon-[dir]-[name]",
      }),
      viteMockServe({
        // default
        mockPath: "mock", // 设置模拟数据的存储文件夹
        watchFiles: true, // 设置是否监听文件变化
        enable: true, // 是否启用 mock 功能
      }),
    ],
    resolve: {
      alias: {
        "@": path.resolve("./src"),
      },
    },
    //scss全局变量的配置
    css: {
      preprocessorOptions: {
        scss: {
          javascriptEnabled: true,
          additionalData: '@import "./src/styles/variable.scss";',
        },
      },
    },
    //代理跨域
    server: {
      proxy: {
        [env.VITE_APP_BASE_API]: {
          target: env.VITE_SERVE, //获取数据的服务器的地址设置
          changeOrigin: true, //需要代理跨域
          rewrite: (path) => path.replace(/^\/api/, ""), //路径重写
        },
      },
    },
  };
});
